#include "stm32f10x.h"                  // Device header
#include "MyI2C.h"
#include "MLX90393.h"
#include "Delay.h"

/**
  * @brief  向MLX90393寄存器写入数据
  * @param  slaveAddr: 从机7位地址左移1位后的写地址（如0x0C）
  * @param  regAddr: 目标寄存器地址（如0x00）
  * @param  data: 要写入的数据
  * @retval 0: 写入成功 1: 写入失败（无应答）
  */
void MLX90393_WriteReg(uint8_t slaveAddr, uint8_t data_H, uint8_t data_L, uint8_t addr)
{   
    uint8_t Data_3;
    // 1. 发送I2C起始信号
    MyI2C_Start();
    
    // 2. 发送从机地址（7位地址左移1位，最低位为0表示写操作）
    MyI2C_SendByte(slaveAddr);
    MyI2C_ReceiveAck(); 
    MyI2C_SendByte(0x60);
    MyI2C_ReceiveAck(); 
    // 4. 发送数据
    MyI2C_SendByte(data_H);//高字节
    MyI2C_ReceiveAck();
    MyI2C_SendByte(data_L);//低字节
    MyI2C_ReceiveAck(); 
    MyI2C_SendByte(addr<<2);
    MyI2C_ReceiveAck();
    MyI2C_Start();  
    MyI2C_SendByte(slaveAddr | 0x01);  
    MyI2C_ReceiveAck();
    Data_3 = MyI2C_ReceiveByte();
    MyI2C_SendAck(1);
    MyI2C_Stop();
}

/**
  * @brief  从MLX90393寄存器读取数据
  * @param  slaveAddr: 从机7位地址左移1位后的写地址（如0x0C）
  * @param  regAddr: 目标寄存器地址（如0x00）
  * @retval 寄存器数据
  */
uint8_t MLX90393_ReadReg(uint8_t slaveAddr, uint8_t regAddr)
{
  uint8_t Data;
	
	MyI2C_Start();
	MyI2C_SendByte(slaveAddr);
	MyI2C_ReceiveAck();
	MyI2C_SendByte(regAddr);
	MyI2C_ReceiveAck();
	
	MyI2C_Start();
	MyI2C_SendByte(slaveAddr | 0x01);
	MyI2C_ReceiveAck();
	Data = MyI2C_ReceiveByte();
	MyI2C_SendAck(1);
	MyI2C_Stop();
	
	return Data;
}

//初始化配置，返回状态位
uint8_t MLX90393_Init(uint8_t slaveAddr) {
    MyI2C_Init();

    MyI2C_Start();//rest模式
    MyI2C_SendByte(slaveAddr);
    MyI2C_ReceiveAck(); 
    MyI2C_SendByte(0x80);
    MyI2C_ReceiveAck(); 
    MyI2C_Stop();
    
		uint16_t burst_sel = 0x05c5; 
		uint8_t high_byte = (burst_sel >> 8) & 0xFF; 
		uint8_t low_byte = burst_sel & 0xFF;  
		MLX90393_WriteReg(slaveAddr, high_byte, low_byte, 0x01);
	
	
		uint16_t target_data =  0x1012;
		uint8_t high_byte_1 = (target_data >> 8) & 0xFF;
		uint8_t low_byte_1 = target_data & 0xFF; 
		MLX90393_WriteReg(slaveAddr, high_byte_1, low_byte_1, 0x02);
		
    uint16_t gain_sel = 0x5; // 中等增益
    uint16_t reg_value_3 = gain_sel << 4; // 仅高7位有效，低9位为0
    
    // 写入寄存器0x00（实际地址为0x00 << 2 = 0x00）
    MLX90393_WriteReg(slaveAddr, (reg_value_3 >> 8) & 0xFF, reg_value_3 & 0xFF, 0x00);

    uint8_t status = MLX90393_ReadReg(slaveAddr, 0x1E);


    return status;
}

void MLX90393_GetData(uint8_t slaveAddr,int16_t *x, int16_t *y, int16_t *z)
{
	uint8_t Data_1, Data_2,Data_3,Data_4,Data_5,Data_6,Data_7;
	MyI2C_Start();
	MyI2C_SendByte(slaveAddr);
	MyI2C_ReceiveAck();
	MyI2C_SendByte(0x4E);
	MyI2C_ReceiveAck();
	
	MyI2C_Start();
	MyI2C_SendByte(slaveAddr | 0x01);
	MyI2C_ReceiveAck();

	Data_1 = MyI2C_ReceiveByte();//stauts
	MyI2C_SendAck(0);
    Data_2 = MyI2C_ReceiveByte();//x[15:8]
    MyI2C_SendAck(0);
    Data_3 = MyI2C_ReceiveByte();//x[7:0]
	MyI2C_SendAck(0);
    Data_4 = MyI2C_ReceiveByte();//y[15:8]
    MyI2C_SendAck(0);
    Data_5 = MyI2C_ReceiveByte();//y[7:0]  
    MyI2C_SendAck(0);
    Data_6 = MyI2C_ReceiveByte();//z[15:8]  
    MyI2C_SendAck(0);
    Data_7 = MyI2C_ReceiveByte();//z[7:0]  
    MyI2C_SendAck(1);
	MyI2C_Stop();
    // 组合16位数据并处理正负号
    *x = (int16_t)((uint16_t)Data_2 << 8 | Data_3);
    *y = (int16_t)((uint16_t)Data_4 << 8 | Data_5);
    *z = (int16_t)((uint16_t)Data_6 << 8 | Data_7);
}


